<template>
    <div class="header-wrap">
        <router-link
          tag="div"
          to="/"
          class="header-abs"
          v-show="showAbs"
        >
          <span class="iconfont">&#xe624;</span>
        </router-link>
        <div
          class="header-fixed"
          v-show="!showAbs"
          :style="opacityStyle"
        >
          <router-link to="/">
            <div class="header-left">
                <span class="iconfont">&#xe624;</span>
            </div>
          </router-link>
          景点详情
        </div>
    </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      console.log('scroll')
      const top = document.documentElement.scrollTop
      if (top > 60) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showAbs = false
      } else {
        this.showAbs = true
      }
      console.log(document.documentElement.scrollTop)
    }
  },
  activated () {
    // 每一次页面展示的时候执行activated 生命周期狗子
    window.addEventListener('scroll', this.handleScroll)
  },
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll)
  },
  created () {
    // 如果在app.vue中使用了exclude，那么就不会执行activated，但是执行了created
    window.addEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
@import '~s/varibles.styl'
.header-abs
  position absolute
  left .2rem
  top .2rem
  width .7rem
  height .7rem
  line-height .7rem
  text-align center
  background rgba(0,0,0,0.8)
  border-radius .4rem
  .iconfont
    color #ffffff
    font-size .36rem
.header-fixed
    position fixed
    top 0
    left 0
    right 0
    z-index 99
    height $headerHeight
    line-height $headerHeight
    text-align center
    color #fff
    font-size .32rem
    background-color: $ThemeColor
    .header-left
        position absolute
        top 0
        left 0
        width .64rem
        text-align center
        font-size .4rem
        color #fff
</style>
